<template>
  <div class="app-container">
    <el-form :inline="true" :model="listQuery" size="small" label-width="100">
      <el-form-item label="广告名称：">
        <el-input v-model="listQuery.name" class="input-width" placeholder="广告名称"></el-input>
      </el-form-item>
      <el-form-item label="广告位置：">
        <el-select v-model="listQuery.type" placeholder="全部" clearable class="input-width">
          <el-option v-for="item in ADVEERT_TYPE_MAP" :key="item.value" :label="item.label" :value="item.value"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="到期时间：">
        <el-date-picker class="input-width" v-model="listQuery.endTime" value-format="YYYY-MM-DD" type="date" placeholder="请选择时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearchList" size="small"> 搜索 </el-button>
        <el-button @click="handleResetSearch" size="small"> 重置 </el-button>
        <el-button size="small" class="btn-add" @click="handleAdd">添加广告</el-button>
      </el-form-item>
    </el-form>
    <div class="table-container">
      <el-table
        ref="homeAdvertiseTable"
        height="100%"
        :data="list"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
      >
        <el-table-column type="selection" width="60" align="center"></el-table-column>
        <el-table-column label="编号" width="80" align="center">
          <template #default="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="广告名称" min-width="180" show-overflow-tooltip align="center">
          <template #default="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="广告位置" width="120" align="center">
          <template #default="scope">{{ ADVEERT_TYPE_DICT[scope.row.type] }}</template>
        </el-table-column>
        <el-table-column label="广告图片" width="140" align="center">
          <template #default="scope"><img style="height: 40px" :src="scope.row.pic" /></template>
        </el-table-column>
        <el-table-column label="开始时间" width="160" align="center">
          <template #default="scope">
            {{ formatDateTime(scope.row.startTime) }}
          </template>
        </el-table-column>
        <el-table-column label="到期时间" width="160" align="center">
          <template #default="scope">
            {{ formatDateTime(scope.row.endTime) }}
          </template>
        </el-table-column>
        <el-table-column label="上线/下线" width="120" align="center">
          <template #default="scope">
            <el-switch
              v-model="scope.row.status"
              size="small"
              :active-value="1"
              :inactive-value="0"
              @change="handleUpdateStatus(scope.$index, scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="点击次数" width="120" align="center">
          <template #default="scope">{{ scope.row.clickCount }}</template>
        </el-table-column>
        <el-table-column label="生成订单" width="120" align="center">
          <template #default="scope">{{ scope.row.orderCount }}</template>
        </el-table-column>
        <el-table-column label="操作" width="120" align="center">
          <template #default="scope">
            <el-button size="small" link type="primary" @click="handleUpdate(scope.row)">编辑 </el-button>
            <el-button size="small" link type="danger" @click="handleDelete(scope.row)">删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="batch-operate-container">
      <el-select size="small" v-model="operateType" placeholder="批量操作">
        <el-option v-for="item in operates" :key="item.value" :label="item.label" :value="item.value"> </el-option>
      </el-select>
      <el-button class="search-button" @click="handleBatchOperate" type="primary" size="small"> 确定 </el-button>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5, 10, 15]"
        v-model:current-page="listQuery.pageNum"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script setup lang="ts">
import { fetchList, updateStatus, deleteHomeAdvertise as deleteHomeAdvertiseApi } from '@/api/homeAdvertise'
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'
import type { Advertise, AdvertisePageDto } from '@/models/advertise'
import { ADVEERT_TYPE_MAP, ADVEERT_TYPE_DICT } from '@/constant'
import { formatDateTime } from '@/utils/format'

defineOptions({ name: 'HomeAdvertiseList' })
const router = useRouter()
const listQuery = ref<AdvertisePageDto>({ pageNum: 1, pageSize: 10 })
const list = ref<Advertise[]>([])
const total = ref(0)
const listLoading = ref(false)
const multipleSelection = ref<Advertise[]>([])
const operateType = ref(null)
const operates = ref([{ label: '删除', value: 0 }])
getList()

function handleResetSearch() {
  listQuery.value = { pageNum: 1, pageSize: 10 }
}
function handleSearchList() {
  listQuery.value.pageNum = 1
  getList()
}
function handleSelectionChange(val: Advertise[]) {
  multipleSelection.value = val
}
function handleSizeChange(val: number) {
  listQuery.value.pageNum = 1
  listQuery.value.pageSize = val
  getList()
}
function handleCurrentChange(val: number) {
  listQuery.value.pageNum = val
  getList()
}
async function handleUpdateStatus(index: number, row: Advertise) {
  try {
    await updateStatus(row.id as number, { status: row.status as 0 | 1 })
    ElMessage.success('修改成功!')
    getList()
  } catch {
    row.status = row.status === 1 ? 0 : 1
  }
}
function handleDelete(row: Advertise) {
  deleteHomeAdvertise(row.id + '')
}
function handleBatchOperate() {
  if (multipleSelection.value.length < 1) {
    ElMessage.warning('请选择一条记录')
    return
  }
  if (!operateType.value) {
    ElMessage.warning('请选择操作类型')
    return
  }
  const ids = multipleSelection.value.map((item) => item.id as number).join(',')
  deleteHomeAdvertise(ids)
}
function handleAdd() {
  router.push({ path: '/sms/advertiseUpsert' })
}
function handleUpdate(row: Advertise) {
  router.push({ path: '/sms/advertiseUpsert', query: { id: row.id } })
}
async function getList() {
  listLoading.value = true
  const response = await fetchList(listQuery.value)
  listLoading.value = false
  list.value = response.data.data.list
  total.value = response.data.data.total
}
async function deleteHomeAdvertise(ids: string) {
  await ElMessageBox.confirm('是否要删除该广告?', '提示', { type: 'warning' })
  await deleteHomeAdvertiseApi({ ids })
  getList()
  ElMessage.success('删除成功!')
}
</script>
<style scoped>
.input-width {
  width: 203px;
}
</style>
